
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>十一、目标查找 · 计算机笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="海拉尔的三角">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro-fixed/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-ancre-navigation/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-insert-logo-link-website/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-flexible-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-auto-scroll-table/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex-pro/katex.min.css">
                
            
                
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-sectionx/sectionx.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-terminull-light/terminull.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="tree_withNum.html" />
    
    
    <link rel="prev" href="array_list_withNum.html" />
    

    
        <link rel="shortcut icon" href="image/ico.png" type="image/x-icon">
    
    
    
    
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="image/ico.png">
    
    
        
    
    
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://github.com/spite-triangle" target="_blank" class="custom-link">Github</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../cpp.html">
            
                <a href="../cpp.html">
            
                    
                    C/C++
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="foundation_withNum.html">
            
                <a href="foundation_withNum.html">
            
                    
                    一、基础概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="keyword_withNum.html">
            
                <a href="keyword_withNum.html">
            
                    
                    二、关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="point_reference_withNum.html">
            
                <a href="point_reference_withNum.html">
            
                    
                    三、指针与引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="oop_withNum.html">
            
                <a href="oop_withNum.html">
            
                    
                    四、面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="memory_withNum.html">
            
                <a href="memory_withNum.html">
            
                    
                    五、内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="process_withNum.html">
            
                <a href="process_withNum.html">
            
                    
                    六、进程与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="locks_withNum.html">
            
                <a href="locks_withNum.html">
            
                    
                    七、锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.8" data-path="stl_withNum.html">
            
                <a href="stl_withNum.html">
            
                    
                    八、STL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.9" data-path="dataStruction_withNum.html">
            
                <a href="dataStruction_withNum.html">
            
                    
                    九、数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.10" data-path="array_list_withNum.html">
            
                <a href="array_list_withNum.html">
            
                    
                    十、数组链表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.1.11" data-path="find_withNum.html">
            
                <a href="find_withNum.html">
            
                    
                    十一、目标查找
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.12" data-path="tree_withNum.html">
            
                <a href="tree_withNum.html">
            
                    
                    十二、树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.13" data-path="binaryTree_withNum.html">
            
                <a href="binaryTree_withNum.html">
            
                    
                    十三、二叉树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.14" data-path="dynamicProgram_withNum.html">
            
                <a href="dynamicProgram_withNum.html">
            
                    
                    十四、动态规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.15" data-path="backtracking_withNum.html">
            
                <a href="backtracking_withNum.html">
            
                    
                    十五、回溯算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.16" data-path="monotoneStack_withNum.html">
            
                <a href="monotoneStack_withNum.html">
            
                    
                    十六、单调栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.17" data-path="toollib_withNum.html">
            
                <a href="toollib_withNum.html">
            
                    
                    附录 工具库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../MySQL/mysql.html">
            
                <a href="../../MySQL/mysql.html">
            
                    
                    MySQL
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../MySQL/chapter/introduction_withNum.html">
            
                <a href="../../MySQL/chapter/introduction_withNum.html">
            
                    
                    一、简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../MySQL/chapter/foundation_withNum.html">
            
                <a href="../../MySQL/chapter/foundation_withNum.html">
            
                    
                    二、控制台操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../MySQL/chapter/operatorDB_withNum.html">
            
                <a href="../../MySQL/chapter/operatorDB_withNum.html">
            
                    
                    三、数据库、表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../../MySQL/chapter/operatorData_withNum.html">
            
                <a href="../../MySQL/chapter/operatorData_withNum.html">
            
                    
                    四、数据操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../../MySQL/chapter/transaction_withNum.html">
            
                <a href="../../MySQL/chapter/transaction_withNum.html">
            
                    
                    五、事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../../MySQL/chapter/index_withNum.html">
            
                <a href="../../MySQL/chapter/index_withNum.html">
            
                    
                    六、索引
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.7" data-path="../../MySQL/chapter/authority_withNum.html">
            
                <a href="../../MySQL/chapter/authority_withNum.html">
            
                    
                    七、权限管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.8" data-path="../../MySQL/chapter/backup_withNum.html">
            
                <a href="../../MySQL/chapter/backup_withNum.html">
            
                    
                    八、备份
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../linux/linux.html">
            
                <a href="../../linux/linux.html">
            
                    
                    Linux
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../../linux/chapter/foundation_withNum.html">
            
                <a href="../../linux/chapter/foundation_withNum.html">
            
                    
                    一、原理知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../../linux/chapter/command_withNum.html">
            
                <a href="../../linux/chapter/command_withNum.html">
            
                    
                    二、指令
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../tcp_udp/tcp_upd.html">
            
                <a href="../../tcp_udp/tcp_upd.html">
            
                    
                    网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../../tcp_udp/chapter/foundation_withNum.html">
            
                <a href="../../tcp_udp/chapter/foundation_withNum.html">
            
                    
                    一、网络通讯基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../../tcp_udp/chapter/tcp_udp_withNum.html">
            
                <a href="../../tcp_udp/chapter/tcp_udp_withNum.html">
            
                    
                    二、tcp/udp
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >十一、目标查找</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><span class="title-icon "></span><a href="#&#x67E5;&#x627E;&#x95EE;&#x9898;"><b></b>&#x67E5;&#x627E;&#x95EE;&#x9898;</a></li><li><span class="title-icon "></span><a href="#1-&#x4E8C;&#x5206;&#x6CD5;"><b></b>1. &#x4E8C;&#x5206;&#x6CD5;</a></li><ul><li><span class="title-icon "></span><a href="#11-&#x57FA;&#x672C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><b></b>1.1. &#x57FA;&#x672C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;</a></li><li><span class="title-icon "></span><a href="#12-&#x5BFB;&#x627E;&#x5DE6;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><b></b>1.2. &#x5BFB;&#x627E;&#x5DE6;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;</a></li><li><span class="title-icon "></span><a href="#13-&#x5BFB;&#x627E;&#x53F3;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><b></b>1.3. &#x5BFB;&#x627E;&#x53F3;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;</a></li></ul><li><span class="title-icon "></span><a href="#2-two-sum"><b></b>2. two sum</a></li><ul><li><span class="title-icon "></span><a href="#21-&#x95EE;&#x9898;"><b></b>2.1. &#x95EE;&#x9898;</a></li><li><span class="title-icon "></span><a href="#22-&#x53CC;&#x6307;&#x9488;&#x66B4;&#x529B;&#x679A;&#x4E3E;"><b></b>2.2. &#x53CC;&#x6307;&#x9488;&#x66B4;&#x529B;&#x679A;&#x4E3E;</a></li><li><span class="title-icon "></span><a href="#23-&#x54C8;&#x897F;&#x8868;&#x67E5;&#x8BE2;&#x7B2C;&#x4E8C;&#x4E2A;&#x503C;"><b></b>2.3. &#x54C8;&#x897F;&#x8868;&#x67E5;&#x8BE2;&#x7B2C;&#x4E8C;&#x4E2A;&#x503C;</a></li><li><span class="title-icon "></span><a href="#24-&#x53CC;&#x6307;&#x9488;&#xFF0C;&#x5347;&#x5E8F;&#x5217;&#xFF0C;&#x6C42;&#x4E24;&#x6570;&#x4E4B;&#x548C;"><b></b>2.4. &#x53CC;&#x6307;&#x9488;&#xFF0C;&#x5347;&#x5E8F;&#x5217;&#xFF0C;&#x6C42;&#x4E24;&#x6570;&#x4E4B;&#x548C;</a></li><li><span class="title-icon "></span><a href="#25-&#x8F93;&#x51FA;&#x6240;&#x6709;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x7EC4;&#x5408;"><b></b>2.5. &#x8F93;&#x51FA;&#x6240;&#x6709;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x7EC4;&#x5408;</a></li><li><span class="title-icon "></span><a href="#26-3sum-&#x95EE;&#x9898;"><b></b>2.6. 3Sum &#x95EE;&#x9898;</a></li></ul><li><span class="title-icon "></span><a href="#3-&#x5220;&#x9664;&#x67E5;&#x627E;&#x6570;&#x636E;o1"><b></b>3. &#x5220;&#x9664;/&#x67E5;&#x627E;&#x6570;&#x636E;O(1)</a></li><li><span class="title-icon "></span><a href="#4-&#x533A;&#x95F4;&#x95EE;&#x9898;"><b></b>4. &#x533A;&#x95F4;&#x95EE;&#x9898;</a></li><ul><li><span class="title-icon "></span><a href="#41-&#x89E3;&#x9898;&#x6280;&#x5DE7;"><b></b>4.1. &#x89E3;&#x9898;&#x6280;&#x5DE7;</a></li><li><span class="title-icon "></span><a href="#42-&#x533A;&#x95F4;&#x8986;&#x76D6;&#x95EE;&#x9898;"><b></b>4.2. &#x533A;&#x95F4;&#x8986;&#x76D6;&#x95EE;&#x9898;</a></li><li><span class="title-icon "></span><a href="#43-&#x533A;&#x95F4;&#x5408;&#x5E76;"><b></b>4.3. &#x533A;&#x95F4;&#x5408;&#x5E76;</a></li><li><span class="title-icon "></span><a href="#44-&#x533A;&#x95F4;&#x4EA4;&#x96C6;"><b></b>4.4. &#x533A;&#x95F4;&#x4EA4;&#x96C6;</a></li><li><span class="title-icon "></span><a href="#45-&#x7ED9;&#x5B9A;&#x4E24;&#x77E9;&#x5F62;&#xFF0C;&#x6C42;&#x76F8;&#x4EA4;&#x9762;&#x79EF;"><b></b>4.5. &#x7ED9;&#x5B9A;&#x4E24;&#x77E9;&#x5F62;&#xFF0C;&#x6C42;&#x76F8;&#x4EA4;&#x9762;&#x79EF;</a></li></ul><li><span class="title-icon "></span><a href="#5-&#x4E8C;&#x5206;&#x6CD5;&#x5A01;&#x529B;&#x52A0;&#x5F3A;"><b></b>5. &#x4E8C;&#x5206;&#x6CD5;&#x5A01;&#x529B;&#x52A0;&#x5F3A;</a></li><ul><li><span class="title-icon "></span><a href="#51-&#x73C2;&#x73C2;&#x5403;&#x9999;&#x8549;"><b></b>5.1. &#x73C2;&#x73C2;&#x5403;&#x9999;&#x8549;</a></li><li><span class="title-icon "></span><a href="#52-&#x8FD0;&#x8F93;&#x8D27;&#x7269;"><b></b>5.2. &#x8FD0;&#x8F93;&#x8D27;&#x7269;</a></li><li><span class="title-icon "></span><a href="#53-&#x5206;&#x5272;&#x6570;&#x7EC4;&#x7684;&#x6700;&#x5927;&#x503C;"><b></b>5.3. &#x5206;&#x5272;&#x6570;&#x7EC4;&#x7684;&#x6700;&#x5927;&#x503C;</a></li></ul><li><span class="title-icon "></span><a href="#6-&#x6ED1;&#x52A8;&#x7A97;&#x53E3;"><b></b>6. &#x6ED1;&#x52A8;&#x7A97;&#x53E3;</a></li><ul><li><span class="title-icon "></span><a href="#61-&#x6A21;&#x677F;"><b></b>6.1. &#x6A21;&#x677F;</a></li><li><span class="title-icon "></span><a href="#62-&#x6700;&#x5C0F;&#x8986;&#x76D6;&#x5B50;&#x4E32;"><b></b>6.2. &#x6700;&#x5C0F;&#x8986;&#x76D6;&#x5B50;&#x4E32;</a></li><li><span class="title-icon "></span><a href="#63-&#x5B57;&#x7B26;&#x4E32;&#x6392;&#x5217;"><b></b>6.3. &#x5B57;&#x7B26;&#x4E32;&#x6392;&#x5217;</a></li><li><span class="title-icon "></span><a href="#64-&#x627E;&#x6240;&#x6709;&#x5B57;&#x6BCD;&#x5F02;&#x4F4D;&#x8BCD;"><b></b>6.4. &#x627E;&#x6240;&#x6709;&#x5B57;&#x6BCD;&#x5F02;&#x4F4D;&#x8BCD;</a></li><li><span class="title-icon "></span><a href="#65-&#x6700;&#x957F;&#x65E0;&#x91CD;&#x590D;&#x5B50;&#x4E32;"><b></b>6.5. &#x6700;&#x957F;&#x65E0;&#x91CD;&#x590D;&#x5B50;&#x4E32;</a></li></ul></ul></div><a href="#&#x67E5;&#x627E;&#x95EE;&#x9898;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 style="font-size:60px;text-align:center;" id="&#x67E5;&#x627E;&#x95EE;&#x9898;"><a name="&#x67E5;&#x627E;&#x95EE;&#x9898;" class="anchor-navigation-ex-anchor" href="#&#x67E5;&#x627E;&#x95EE;&#x9898;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x67E5;&#x627E;&#x95EE;&#x9898;</h1>

<h1 id="1-&#x4E8C;&#x5206;&#x6CD5;"><a name="1-&#x4E8C;&#x5206;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#1-&#x4E8C;&#x5206;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x4E8C;&#x5206;&#x6CD5;</h1>
<h2 id="11-&#x57FA;&#x672C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><a name="11-&#x57FA;&#x672C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;" class="anchor-navigation-ex-anchor" href="#11-&#x57FA;&#x672C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. &#x57FA;&#x672C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;</h2>
<ul>
<li><strong><code>while(left &lt;= right)</code>&#x4E00;&#x6B21;&#x641C;&#x7D22;&#x7684;&#x533A;&#x95F4;&#x662F;&#xFF1A;<code>[left, right]</code></strong></li>
<li><strong><code>while(left &lt;= right)</code> &#x7EC8;&#x6B62;&#x6761;&#x4EF6;&#xFF1A;<code>left == right + 1</code></strong>: &#x5C31;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#x4F1A;&#x7EC8;&#x6B62;&#xFF0C;1&#xFF09;<code>right</code>&#x51CF;&#x8FC7;&#x5934;&#xFF1B;2&#xFF09;<code>left</code>&#x52A0;&#x8FC7;&#x5934;</li>
</ul>
<pre class="language-"><code class="lang-java"><span class="token keyword">int</span> <span class="token function">binarySearch</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">,</span> <span class="token keyword">int</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> right <span class="token operator">=</span> nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// &#x6CE8;&#x610F;</span>
        <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;=</span> right<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> mid <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">==</span> target<span class="token punctuation">)</span>
                <span class="token keyword">return</span> mid<span class="token punctuation">;</span>
            <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">&lt;</span> target<span class="token punctuation">)</span>
                left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// &#x6CE8;&#x610F;</span>
            <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">&gt;</span> target<span class="token punctuation">)</span>
                right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// &#x6CE8;&#x610F;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>[!note|style:flat]
<strong>&#x6CE8;&#xFF1A;</strong>
<strong><code>nums = [1,2,2,2,3];targe = 2;</code>&#x627E;&#x76EE;&#x6807;&#x7684;&#x8FB9;&#x754C;&#xFF0C;&#x666E;&#x901A;&#x4E8C;&#x5206;&#x6CD5;&#x65E0;&#x6CD5;&#x5B9E;&#x73B0;&#x3002;</strong></p>
</blockquote>
<h2 id="12-&#x5BFB;&#x627E;&#x5DE6;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><a name="12-&#x5BFB;&#x627E;&#x5DE6;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;" class="anchor-navigation-ex-anchor" href="#12-&#x5BFB;&#x627E;&#x5DE6;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. &#x5BFB;&#x627E;&#x5DE6;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;</h2>
<ul>
<li><strong><code>while(left &lt;= right)</code>&#x4E00;&#x6B21;&#x641C;&#x7D22;&#x7684;&#x533A;&#x95F4;&#x662F;&#xFF1A;<code>[left, right]</code></strong></li>
<li><strong><code>while(left &lt;= right)</code> &#x7EC8;&#x6B62;&#x6761;&#x4EF6;&#xFF1A;<code>left == right + 1</code></strong>: &#x5C31;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#x4F1A;&#x7EC8;&#x6B62;&#xFF0C;1&#xFF09;<code>right</code>&#x51CF;&#x8FC7;&#x5934;&#xFF1B;2&#xFF09;<code>left</code>&#x52A0;&#x8FC7;&#x5934;</li>
<li><strong><code>left = [0,nums.length]</code></strong></li>
<li><span style="color:red;font-weight:bold"> &#x8FD4;&#x56DE;&#x503C;&#x542B;&#x4E49;&#xFF1A;<code>nums</code>&#x4E2D;&#x5C0F;&#x4E8E;<code>target</code>&#x7684;&#x503C;&#x7684;&#x4E2A;&#x6570; </span></li>
<li><span style="color:red;font-weight:bold"> <code>nums</code>&#x5FAA;&#x5E8F;&#x6392;&#x5217;&#xFF0C;<code>target</code>&#x5728;&#x6570;&#x7EC4;<code>nums</code>&#x4E2D;&#x3002; </span></li>
</ul>
<pre class="language-"><code class="lang-java"><span class="token keyword">int</span> <span class="token function">left_bound</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">,</span> <span class="token keyword">int</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> right <span class="token operator">=</span> nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;=</span> right<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> mid <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">&lt;</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">&gt;</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// &#x522B;&#x8FD4;&#x56DE;&#xFF0C;&#x9501;&#x5B9A;&#x5DE6;&#x4FA7;&#x8FB9;&#x754C;</span>
                right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// &#x6700;&#x540E;&#x8981;&#x68C0;&#x67E5; left &#x8D8A;&#x754C;&#x7684;&#x60C5;&#x51B5;&#xFF0C;target&#x503C;&#x4E5F;&#x4E0D;&#x5728;&#x6570;&#x7EC4;&#x4E2D;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>left <span class="token operator">&gt;=</span> nums<span class="token punctuation">.</span>length <span class="token operator">||</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">!=</span> target<span class="token punctuation">)</span>
            <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> left<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="13-&#x5BFB;&#x627E;&#x53F3;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><a name="13-&#x5BFB;&#x627E;&#x53F3;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;" class="anchor-navigation-ex-anchor" href="#13-&#x5BFB;&#x627E;&#x53F3;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. &#x5BFB;&#x627E;&#x53F3;&#x4FA7;&#x8FB9;&#x754C;&#x7684;&#x4E8C;&#x5206;&#x641C;&#x7D22;</h2>
<ul>
<li><span style="color:red;font-weight:bold"> &#x8FD4;&#x56DE;&#x503C;&#x542B;&#x4E49;&#xFF1A;<code>nums</code>&#x4E2D;&#x5927;&#x4E8E;<code>target</code>&#x7684;&#x503C;&#x7684;&#x4E2A;&#x6570; </span></li>
<li><span style="color:red;font-weight:bold"> <code>nums</code>&#x5FAA;&#x5E8F;&#x6392;&#x5217;&#xFF0C;<code>target</code>&#x5728;&#x6570;&#x7EC4;<code>nums</code>&#x4E2D;&#x3002; </span></li>
</ul>
<pre class="language-"><code class="lang-java"><span class="token keyword">int</span> <span class="token function">right_bound</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">,</span> <span class="token keyword">int</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> right <span class="token operator">=</span> nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;=</span> right<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> mid <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">&lt;</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">&gt;</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x522B;&#x8FD4;&#x56DE;&#xFF0C;&#x9501;&#x5B9A;&#x53F3;&#x4FA7;&#x8FB9;&#x754C;</span>
            left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x6700;&#x540E;&#x8981;&#x68C0;&#x67E5; right &#x8D8A;&#x754C;&#x7684;&#x60C5;&#x51B5;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>right <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">||</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span> <span class="token operator">!=</span> target<span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> right<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><strong>&#x6CE8;&#xFF1A;</strong></p>
<ul>
<li><strong><code>while(left &lt; right)</code> &#x7ED3;&#x675F;&#x6761;&#x4EF6;&#x4E3A; <code>left == right</code></strong></li>
<li><strong><code>while(left &lt; right)</code>&#x4E00;&#x6B21;&#x641C;&#x7D22;&#x7684;&#x533A;&#x95F4;&#x662F;&#xFF1A;<code>[left, right)</code></strong></li>
<li><strong><code>while(left &lt; right)</code>&#x51B3;&#x5B9A;&#x4E86;<code>left = mid + 1;right = mid;</code>&#x4E0B;&#x4E00;&#x6B21;&#x7684;&#x641C;&#x7D22;&#x533A;&#x95F4;&#x5C31;&#x662F;&#xFF1A;<code>[left,mid)</code></strong></li>
<li><strong><code>[left, right)</code>&#x5C31;&#x786E;&#x5B9A;&#x4E86;&#x521D;&#x59CB;&#x6761;&#x4EF6;&#xFF1A;<code>left = 0, right = nums.length</code></strong></li>
</ul>
<pre class="language-"><code class="lang-java"><span class="token keyword">int</span> <span class="token function">right_bound</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">,</span> <span class="token keyword">int</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> right <span class="token operator">=</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span>

    <span class="token keyword">while</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> mid <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// &#x6CE8;&#x610F;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">&lt;</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">&gt;</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            right <span class="token operator">=</span> mid<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>left <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> nums<span class="token punctuation">[</span>left <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">!=</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> left <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// &#x6CE8;&#x610F;</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="2-two-sum"><a name="2-two-sum" class="anchor-navigation-ex-anchor" href="#2-two-sum"><i class="fa fa-link" aria-hidden="true"></i></a>2. two sum</h1>
<h2 id="21-&#x95EE;&#x9898;"><a name="21-&#x95EE;&#x9898;" class="anchor-navigation-ex-anchor" href="#21-&#x95EE;&#x9898;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1. &#x95EE;&#x9898;</h2>
<blockquote>
<p>&#x7ED9;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x548C;&#x4E00;&#x4E2A;&#x6574;&#x6570;<code>target</code>&#xFF0C;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x6570;&#x7EC4;&#x4E2D;&#x5B58;&#x5728;&#x4E24;&#x4E2A;&#x6570;&#x7684;&#x548C;&#x4E3A;<code>target</code>&#xFF0C;&#x8FD4;&#x56DE;&#x8FD9;&#x4E24;&#x4E2A;&#x6570;&#x7684;&#x7D22;&#x5F15;&#x3002;
&#x4F8B;:
&#x8F93;&#x5165;: <code>nums = [3,1,3,6],target = 6</code>
&#x8FD4;&#x56DE;&#x6570;&#x7EC4;: <code>[0,2]</code></p>
</blockquote>
<h2 id="22-&#x53CC;&#x6307;&#x9488;&#x66B4;&#x529B;&#x679A;&#x4E3E;"><a name="22-&#x53CC;&#x6307;&#x9488;&#x66B4;&#x529B;&#x679A;&#x4E3E;" class="anchor-navigation-ex-anchor" href="#22-&#x53CC;&#x6307;&#x9488;&#x66B4;&#x529B;&#x679A;&#x4E3E;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2. &#x53CC;&#x6307;&#x9488;&#x66B4;&#x529B;&#x679A;&#x4E3E;</h2>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">twoSum</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token operator">*</span> nums<span class="token punctuation">,</span><span class="token keyword">int</span> target<span class="token punctuation">,</span><span class="token keyword">int</span> <span class="token operator">*</span> out<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>size<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
       <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>j <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>size<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span>            <span class="token punctuation">{</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>target <span class="token operator">==</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">+</span>nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                out<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span>
                out<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span>j<span class="token punctuation">;</span>
                <span class="token keyword">return</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>   
        <span class="token punctuation">}</span> 
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<h2 id="23-&#x54C8;&#x897F;&#x8868;&#x67E5;&#x8BE2;&#x7B2C;&#x4E8C;&#x4E2A;&#x503C;"><a name="23-&#x54C8;&#x897F;&#x8868;&#x67E5;&#x8BE2;&#x7B2C;&#x4E8C;&#x4E2A;&#x503C;" class="anchor-navigation-ex-anchor" href="#23-&#x54C8;&#x897F;&#x8868;&#x67E5;&#x8BE2;&#x7B2C;&#x4E8C;&#x4E2A;&#x503C;"><i class="fa fa-link" aria-hidden="true"></i></a>2.3. &#x54C8;&#x897F;&#x8868;&#x67E5;&#x8BE2;&#x7B2C;&#x4E8C;&#x4E2A;&#x503C;</h2>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">twoSumMap</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token operator">*</span>nums<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> target<span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">*</span> out<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// &#x5B58;&#x50A8; &#x503C;-&#x7D22;&#x5F15;</span>
        map<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">&gt;</span> valIndex<span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            valIndex<span class="token punctuation">[</span>nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// &#x5BFB;&#x627E;&#x76EE;&#x6807;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">int</span> other <span class="token operator">=</span> target <span class="token operator">-</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x4ECE; &#x503C;-&#x7D22;&#x5F15; &#x4E2D;&#x627E;&#x53E6;&#x4E00;&#x534A;&#x7684;&#x503C;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>valIndex<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>other<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> valIndex<span class="token punctuation">[</span>other<span class="token punctuation">]</span> <span class="token operator">!=</span> i<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                out<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span>
                out<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span>valIndex<span class="token punctuation">[</span>other<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="24-&#x53CC;&#x6307;&#x9488;&#xFF0C;&#x5347;&#x5E8F;&#x5217;&#xFF0C;&#x6C42;&#x4E24;&#x6570;&#x4E4B;&#x548C;"><a name="24-&#x53CC;&#x6307;&#x9488;&#xFF0C;&#x5347;&#x5E8F;&#x5217;&#xFF0C;&#x6C42;&#x4E24;&#x6570;&#x4E4B;&#x548C;" class="anchor-navigation-ex-anchor" href="#24-&#x53CC;&#x6307;&#x9488;&#xFF0C;&#x5347;&#x5E8F;&#x5217;&#xFF0C;&#x6C42;&#x4E24;&#x6570;&#x4E4B;&#x548C;"><i class="fa fa-link" aria-hidden="true"></i></a>2.4. &#x53CC;&#x6307;&#x9488;&#xFF0C;&#x5347;&#x5E8F;&#x5217;&#xFF0C;&#x6C42;&#x4E24;&#x6570;&#x4E4B;&#x548C;</h2>
<pre class="language-"><code class="lang-java"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">twoSum</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">,</span> <span class="token keyword">int</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> right <span class="token operator">=</span> nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> sum <span class="token operator">=</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">+</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>sum <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// &#x9898;&#x76EE;&#x8981;&#x6C42;&#x7684;&#x7D22;&#x5F15;&#x662F;&#x4ECE; 1 &#x5F00;&#x59CB;&#x7684;</span>
                <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span>left <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> right <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>sum <span class="token operator">&lt;</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                left<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token comment">// &#x8BA9; sum &#x5927;&#x4E00;&#x70B9;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>sum <span class="token operator">&gt;</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                right<span class="token operator">--</span><span class="token punctuation">;</span> <span class="token comment">// &#x8BA9; sum &#x5C0F;&#x4E00;&#x70B9;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="25-&#x8F93;&#x51FA;&#x6240;&#x6709;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x7EC4;&#x5408;"><a name="25-&#x8F93;&#x51FA;&#x6240;&#x6709;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x7EC4;&#x5408;" class="anchor-navigation-ex-anchor" href="#25-&#x8F93;&#x51FA;&#x6240;&#x6709;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x7EC4;&#x5408;"><i class="fa fa-link" aria-hidden="true"></i></a>2.5. &#x8F93;&#x51FA;&#x6240;&#x6709;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x7EC4;&#x5408;</h2>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">twoSum</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>nums<span class="token punctuation">,</span><span class="token keyword">int</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// &#x5148;&#x6392;&#x5E8F;</span>
        <span class="token function">sort</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>nums<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> right <span class="token operator">=</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> 
        <span class="token keyword">int</span> temp<span class="token punctuation">;</span>

        <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">int</span> sum <span class="token operator">=</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">+</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>sum <span class="token operator">&lt;</span> target<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                left <span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>sum <span class="token operator">&gt;</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>
                right <span class="token operator">--</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sum <span class="token operator">==</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>

                cout <span class="token operator">&lt;&lt;</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">&lt;&lt;</span> <span class="token string">&quot; &quot;</span> <span class="token operator">&lt;&lt;</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>

                <span class="token comment">// &#x5DE6;&#x8FB9;&#x91CD;&#x590D;</span>
                temp <span class="token operator">=</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right <span class="token operator">&amp;&amp;</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">==</span> temp<span class="token punctuation">)</span><span class="token punctuation">{</span>
                    left<span class="token operator">++</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>

                <span class="token comment">// &#x53F3;&#x8FB9;&#x91CD;&#x590D;</span>
                temp <span class="token operator">=</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right <span class="token operator">&amp;&amp;</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span> <span class="token operator">==</span> temp<span class="token punctuation">)</span><span class="token punctuation">{</span>
                    right<span class="token operator">--</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> 
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="26-3sum-&#x95EE;&#x9898;"><a name="26-3sum-&#x95EE;&#x9898;" class="anchor-navigation-ex-anchor" href="#26-3sum-&#x95EE;&#x9898;"><i class="fa fa-link" aria-hidden="true"></i></a>2.6. 3Sum &#x95EE;&#x9898;</h2>
<ul>
<li><code>sort()</code>: &#x9996;&#x5148;&#x8FDB;&#x884C;&#x6392;&#x5E8F;</li>
<li>&#x4ECE;&#x5DE6;&#x5411;&#x53F3;&#x4F9D;&#x6B21;&#x904D;&#x5386;&#x6570;&#x7EC4; <code>nums[index]</code></li>
<li><code>target =target - nums[index]</code>: <strong>&#x5C06;3sum&#x95EE;&#x9898;&#x8F6C;&#x4E3A;2sum&#x95EE;&#x9898;&#x3002;</strong></li>
<li><strong><code>nums[index]</code>&#x8FD8;&#x8981;&#x53BB;&#x91CD;&#x590D;</strong></li>
</ul>
<pre class="language-"><code class="lang-cpp">vector<span class="token operator">&lt;</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> <span class="token function">twoSum</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>nums<span class="token punctuation">,</span><span class="token keyword">int</span> start<span class="token punctuation">,</span><span class="token keyword">int</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>

        vector<span class="token operator">&lt;</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> out<span class="token punctuation">;</span>
        <span class="token keyword">int</span> left <span class="token operator">=</span> start<span class="token punctuation">;</span>
        <span class="token keyword">int</span> right <span class="token operator">=</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> 
        <span class="token keyword">int</span> temp<span class="token punctuation">;</span>

        <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">int</span> sum <span class="token operator">=</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">+</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>sum <span class="token operator">&lt;</span> target<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                left <span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>sum <span class="token operator">&gt;</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>
                right <span class="token operator">--</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sum <span class="token operator">==</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>

                out<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token generic-function"><span class="token function">vector</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                out<span class="token punctuation">[</span>out<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                out<span class="token punctuation">[</span>out<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token comment">// &#x5DE6;&#x8FB9;&#x91CD;&#x590D;</span>
                temp <span class="token operator">=</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right <span class="token operator">&amp;&amp;</span> nums<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">==</span> temp<span class="token punctuation">)</span><span class="token punctuation">{</span>
                    left<span class="token operator">++</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>        
                <span class="token comment">// &#x53F3;&#x8FB9;&#x91CD;&#x590D;</span>
                temp <span class="token operator">=</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right <span class="token operator">&amp;&amp;</span> nums<span class="token punctuation">[</span>right<span class="token punctuation">]</span> <span class="token operator">==</span> temp<span class="token punctuation">)</span><span class="token punctuation">{</span>
                    right<span class="token operator">--</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> 
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> out<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    vector<span class="token operator">&lt;</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> <span class="token function">threeSum</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>nums<span class="token punctuation">,</span><span class="token keyword">int</span> target<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// &#x5148;&#x6392;&#x5E8F;</span>
        <span class="token function">sort</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>nums<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        vector<span class="token operator">&lt;</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> out<span class="token punctuation">;</span>
        <span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> temp<span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>index <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">// target &#x51CF;&#x53BB; &#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x5C06; 3sum &#x53D8;&#x6210; 2sum</span>
            vector<span class="token operator">&lt;</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> res <span class="token operator">=</span> <span class="token function">twoSum</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>index<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span> target <span class="token operator">-</span> nums<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">// &#x53BB;&#x91CD;&#x590D;</span>
            temp <span class="token operator">=</span> nums<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>index <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">2</span> <span class="token operator">&amp;&amp;</span> nums<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">==</span> temp<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                index<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// &#x5C06;&#x7B2C;&#x4E09;&#x4E2A;&#x503C;&#x50A8;&#x5B58; </span>
            <span class="token keyword">for</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> item <span class="token operator">:</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
                item<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>temp<span class="token punctuation">)</span><span class="token punctuation">;</span>
                out<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> out<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h1 id="3-&#x5220;&#x9664;&#x67E5;&#x627E;&#x6570;&#x636E;o1"><a name="3-&#x5220;&#x9664;&#x67E5;&#x627E;&#x6570;&#x636E;o1" class="anchor-navigation-ex-anchor" href="#3-&#x5220;&#x9664;&#x67E5;&#x627E;&#x6570;&#x636E;o1"><i class="fa fa-link" aria-hidden="true"></i></a>3. &#x5220;&#x9664;/&#x67E5;&#x627E;&#x6570;&#x636E;O(1)</h1>
<ul>
<li>&#x4F7F;&#x7528;<code>map</code>&#x5B58;&#x50A8;&#x7D22;&#x5F15;&#xFF0C;<code>vector</code>&#x5B58;&#x50A8;&#x6570;&#x636E;<ul>
<li>&#x67E5;&#x627E;: &#x901A;&#x8FC7;<code>map</code>&#x627E;&#x7D22;&#x5F15;&#xFF0C;&#x901A;&#x8FC7;&#x7D22;&#x5F15;&#x53BB;<code>vector</code>&#x62FF;&#x6570;&#x636E;</li>
<li>&#x5220;&#x9664;: &#x5C06;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4E0E;&#x5220;&#x9664;&#x5143;&#x7D20;&#x8FDB;&#x884C;&#x66FF;&#x6362;&#xFF0C;&#x4FEE;&#x6539;<code>map</code>&#xFF0C;&#x7136;&#x540E;&#x518D;&#x5220;&#x9664;&#x7D22;&#x5F15;&#x548C;&#x6570;&#x636E;</li>
</ul>
</li>
</ul>
<h1 id="4-&#x533A;&#x95F4;&#x95EE;&#x9898;"><a name="4-&#x533A;&#x95F4;&#x95EE;&#x9898;" class="anchor-navigation-ex-anchor" href="#4-&#x533A;&#x95F4;&#x95EE;&#x9898;"><i class="fa fa-link" aria-hidden="true"></i></a>4. &#x533A;&#x95F4;&#x95EE;&#x9898;</h1>
<h2 id="41-&#x89E3;&#x9898;&#x6280;&#x5DE7;"><a name="41-&#x89E3;&#x9898;&#x6280;&#x5DE7;" class="anchor-navigation-ex-anchor" href="#41-&#x89E3;&#x9898;&#x6280;&#x5DE7;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1. &#x89E3;&#x9898;&#x6280;&#x5DE7;</h2>
<ol>
<li><strong>&#x7AEF;&#x70B9;&#x6392;&#x5E8F;&#xFF1A;&#x6309;&#x7167;&#x533A;&#x95F4;&#x8D77;&#x70B9;&#x6392;&#x5E8F;&#xFF0C;&#x6216;&#x8005;&#x5148;&#x6309;&#x7167;&#x8D77;&#x70B9;&#x5347;&#x5E8F;&#x6392;&#x5E8F;&#xFF0C;&#x82E5;&#x8D77;&#x70B9;&#x76F8;&#x540C;&#xFF0C;&#x5219;&#x6309;&#x7167;&#x7EC8;&#x70B9;&#x964D;&#x5E8F;&#x6392;&#x5E8F;&#x3002;</strong> <span style="color:red;font-weight:bold"> &#x4E0D;&#x964D;&#x5E8F;&#xFF0C;&#x4F1A;&#x51FA;&#x95EE;&#x9898; </span>
 <a href="../../image/cpp/descendingOrder.jpg" data-lightbox="445af66c-2b8a-4b9b-8bce-e5028821eda1" data-title="descending order"><img src="../../image/cpp/descendingOrder.jpg" alt="descending order"></a></li>
<li><strong>&#x4F5C;&#x56FE;&#xFF1A;&#x5206;&#x522B;&#x8BA8;&#x8BBA;&#x4E00;&#x6B21;&#x6D89;&#x53CA;&#x7684;&#x5FAA;&#x73AF;&#x533A;&#x95F4;&#xFF0C;&#x6709;&#x54EA;&#x4E9B;&#x60C5;&#x51B5;&#xFF0C;&#x9632;&#x6B62;&#x6F0F;&#x6389;&#x3002;</strong> <span style="color:red;font-weight:bold"> &#x533A;&#x95F4;&#x5173;&#x7CFB;&#x6761;&#x4EF6;&#x5F97;&#x627E;&#x5BF9;&#x3002; </span></li>
</ol>
<h2 id="42-&#x533A;&#x95F4;&#x8986;&#x76D6;&#x95EE;&#x9898;"><a name="42-&#x533A;&#x95F4;&#x8986;&#x76D6;&#x95EE;&#x9898;" class="anchor-navigation-ex-anchor" href="#42-&#x533A;&#x95F4;&#x8986;&#x76D6;&#x95EE;&#x9898;"><i class="fa fa-link" aria-hidden="true"></i></a>4.2. &#x533A;&#x95F4;&#x8986;&#x76D6;&#x95EE;&#x9898;</h2>
<p><a href="../../image/cpp/CoveredIntervals.png" data-lightbox="dfbbcb68-b275-4b3d-bf5e-5303ad31a57b" data-title="covered interval"><img src="../../image/cpp/CoveredIntervals.png" alt="covered interval"></a></p>
<pre class="language-"><code class="lang-java"><span class="token keyword">int</span> <span class="token function">removeCoveredIntervals</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> intvs<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x6309;&#x7167;&#x8D77;&#x70B9;&#x5347;&#x5E8F;&#x6392;&#x5217;&#xFF0C;&#x8D77;&#x70B9;&#x76F8;&#x540C;&#x65F6;&#x964D;&#x5E8F;&#x6392;&#x5217;</span>
        <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>intvs<span class="token punctuation">,</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> b<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> b<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">-</span> b<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x5927;&#x533A;&#x95F4;</span>
        <span class="token keyword">int</span> left <span class="token operator">=</span> intvs<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> right <span class="token operator">=</span> intvs<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

        <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> intvs<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> intv <span class="token operator">=</span> intvs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x60C5;&#x51B5;&#x4E00;&#xFF0C;&#x627E;&#x5230;&#x8986;&#x76D6;&#x533A;&#x95F4;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;=</span> intv<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> right <span class="token operator">&gt;=</span> intv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                res<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment">// &#x60C5;&#x51B5;&#x4E8C;&#xFF0C;&#x627E;&#x5230;&#x76F8;&#x4EA4;&#x533A;&#x95F4;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>right <span class="token operator">&gt;=</span> intv<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> right <span class="token operator">&lt;=</span> intv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                left <span class="token operator">=</span> intv<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
                right <span class="token operator">=</span> intv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment">// &#x60C5;&#x51B5;&#x4E09;&#xFF0C;&#x5B8C;&#x5168;&#x4E0D;&#x76F8;&#x4EA4;&#xFF0C;&#x66F4;&#x65B0;&#x8D77;&#x70B9;&#x548C;&#x7EC8;&#x70B9;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>right <span class="token operator">&lt;</span> intv<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                left <span class="token operator">=</span> intv<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
                right <span class="token operator">=</span> intv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> intvs<span class="token punctuation">.</span>length <span class="token operator">-</span> res<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="43-&#x533A;&#x95F4;&#x5408;&#x5E76;"><a name="43-&#x533A;&#x95F4;&#x5408;&#x5E76;" class="anchor-navigation-ex-anchor" href="#43-&#x533A;&#x95F4;&#x5408;&#x5E76;"><i class="fa fa-link" aria-hidden="true"></i></a>4.3. &#x533A;&#x95F4;&#x5408;&#x5E76;</h2>
<p><a href="../../image/cpp/mergeInterval.png" data-lightbox="3ae11436-13b1-437d-8801-c6aa8dcbb63c" data-title="merge interval"><img src="../../image/cpp/mergeInterval.png" alt="merge interval"></a></p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">struct</span> <span class="token class-name">Interval</span><span class="token punctuation">{</span>
        <span class="token keyword">int</span> left<span class="token punctuation">;</span>
        <span class="token keyword">int</span> right<span class="token punctuation">;</span>
        <span class="token function">Interval</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
        <span class="token function">Interval</span><span class="token punctuation">(</span><span class="token keyword">int</span> left<span class="token punctuation">,</span> <span class="token keyword">int</span> right<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>left <span class="token operator">=</span> left<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>right <span class="token operator">=</span> right<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token keyword">class</span> <span class="token class-name">Compare</span><span class="token punctuation">{</span>
    <span class="token keyword">public</span><span class="token operator">:</span>
        <span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">const</span> Interval <span class="token operator">&amp;</span> a<span class="token punctuation">,</span> <span class="token keyword">const</span> Interval <span class="token operator">&amp;</span> b<span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>left <span class="token operator">&lt;</span> b<span class="token punctuation">.</span>left<span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> 
            <span class="token punctuation">}</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>left <span class="token operator">==</span> b<span class="token punctuation">.</span>left<span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>right <span class="token operator">&gt;</span> b<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> 
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>

    vector<span class="token operator">&lt;</span>Interval<span class="token operator">&gt;</span> <span class="token function">mergeIntervals</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>Interval<span class="token operator">&gt;</span><span class="token operator">&amp;</span> intervals<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// &#x6392;&#x5E8F;</span>
        <span class="token function">sort</span><span class="token punctuation">(</span>intervals<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> intervals<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">Compare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        vector<span class="token operator">&lt;</span>Interval<span class="token operator">&gt;</span> res<span class="token punctuation">;</span>
        <span class="token keyword">int</span> left <span class="token operator">=</span> intervals<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>left<span class="token punctuation">;</span>
        <span class="token keyword">int</span> right <span class="token operator">=</span> intervals<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>right<span class="token punctuation">;</span>
        res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Interval</span><span class="token punctuation">(</span>left<span class="token punctuation">,</span> right<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> intervals<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">int</span> curL <span class="token operator">=</span> intervals<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>left<span class="token punctuation">;</span>
            <span class="token keyword">int</span> curR <span class="token operator">=</span> intervals<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>right<span class="token punctuation">;</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>curL <span class="token operator">&gt;=</span> left <span class="token operator">&amp;&amp;</span> curR <span class="token operator">&lt;=</span> right<span class="token punctuation">)</span> <span class="token comment">// &#x5305;&#x542B;</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">continue</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>curL <span class="token operator">&gt;=</span> left <span class="token operator">&amp;&amp;</span> curL <span class="token operator">&lt;=</span> right <span class="token operator">&amp;&amp;</span> curR <span class="token operator">&gt;</span> right<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// &#x76F8;&#x4EA4;</span>
                res<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>right <span class="token operator">=</span> curR<span class="token punctuation">;</span>
                right <span class="token operator">=</span> curR<span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>curL <span class="token operator">&gt;</span> right<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// &#x5206;&#x79BB;</span>
                left <span class="token operator">=</span> curL<span class="token punctuation">;</span>
                right <span class="token operator">=</span> curR<span class="token punctuation">;</span>
                res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Interval</span><span class="token punctuation">(</span>left<span class="token punctuation">,</span> right<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> 
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> res<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="44-&#x533A;&#x95F4;&#x4EA4;&#x96C6;"><a name="44-&#x533A;&#x95F4;&#x4EA4;&#x96C6;" class="anchor-navigation-ex-anchor" href="#44-&#x533A;&#x95F4;&#x4EA4;&#x96C6;"><i class="fa fa-link" aria-hidden="true"></i></a>4.4. &#x533A;&#x95F4;&#x4EA4;&#x96C6;</h2>
<p><a href="../../image/cpp/intersectingInterval.png" data-lightbox="777c048f-35e2-4aaf-b129-8e2b35ac0bf1" data-title="interstion interval"><img src="../../image/cpp/intersectingInterval.png" alt="interstion interval"></a></p>
<ul>
<li><strong>&#x5F52;&#x7EB3;&#x4EA4;&#x96C6;&#xFF1A;<code>!(l1 &gt; r2 || r1 &lt; l2)</code></strong></li>
<li><strong>&#x5F52;&#x7EB3;&#x6307;&#x9488;&#x79FB;&#x52A8;&#x6761;&#x4EF6;&#xFF1A;<code>(r1 &lt; r2)</code></strong></li>
</ul>
<pre class="language-"><code class="lang-cpp">vector<span class="token operator">&lt;</span> Interval <span class="token operator">&gt;</span> <span class="token function">intersection</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>Interval<span class="token operator">&gt;</span><span class="token operator">&amp;</span> inter1<span class="token punctuation">,</span> vector<span class="token operator">&lt;</span>Interval<span class="token operator">&gt;</span><span class="token operator">&amp;</span> inter2<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// &#x5148;&#x6392;&#x5E8F;</span>
        <span class="token function">sort</span><span class="token punctuation">(</span>inter1<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> inter1<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">Compare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">sort</span><span class="token punctuation">(</span>inter2<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> inter2<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">Compare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x533A;&#x95F4;&#x6307;&#x9488;</span>
        <span class="token keyword">int</span> index1<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>index2<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> left<span class="token punctuation">,</span>right<span class="token punctuation">;</span>
        vector<span class="token operator">&lt;</span>Interval<span class="token operator">&gt;</span> res<span class="token punctuation">;</span>

        <span class="token comment">// &#x904D;&#x5386;&#x533A;&#x95F4;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>index1 <span class="token operator">&lt;</span> inter1<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> index2 <span class="token operator">&lt;</span> inter2<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">int</span> l1 <span class="token operator">=</span> inter1<span class="token punctuation">[</span>index1<span class="token punctuation">]</span><span class="token punctuation">.</span>left<span class="token punctuation">;</span>
            <span class="token keyword">int</span> r1 <span class="token operator">=</span> inter1<span class="token punctuation">[</span>index1<span class="token punctuation">]</span><span class="token punctuation">.</span>right<span class="token punctuation">;</span>
            <span class="token keyword">int</span> l2 <span class="token operator">=</span> inter2<span class="token punctuation">[</span>index2<span class="token punctuation">]</span><span class="token punctuation">.</span>left<span class="token punctuation">;</span>
            <span class="token keyword">int</span> r2 <span class="token operator">=</span> inter2<span class="token punctuation">[</span>index2<span class="token punctuation">]</span><span class="token punctuation">.</span>right<span class="token punctuation">;</span>

            <span class="token comment">// &#x76F8;&#x4EA4;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token punctuation">(</span> l1 <span class="token operator">&gt;</span> r2 <span class="token operator">||</span> r1 <span class="token operator">&lt;</span> l2 <span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                left <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>l1<span class="token punctuation">,</span>l2<span class="token punctuation">)</span><span class="token punctuation">;</span>
                right <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>r1<span class="token punctuation">,</span>r2<span class="token punctuation">)</span><span class="token punctuation">;</span>
                res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Interval</span><span class="token punctuation">(</span>left<span class="token punctuation">,</span> right<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// &#x79FB;&#x52A8;&#x6307;&#x9488;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>r1 <span class="token operator">&lt;</span> r2<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                index1<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
                index2<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> res<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="45-&#x7ED9;&#x5B9A;&#x4E24;&#x77E9;&#x5F62;&#xFF0C;&#x6C42;&#x76F8;&#x4EA4;&#x9762;&#x79EF;"><a name="45-&#x7ED9;&#x5B9A;&#x4E24;&#x77E9;&#x5F62;&#xFF0C;&#x6C42;&#x76F8;&#x4EA4;&#x9762;&#x79EF;" class="anchor-navigation-ex-anchor" href="#45-&#x7ED9;&#x5B9A;&#x4E24;&#x77E9;&#x5F62;&#xFF0C;&#x6C42;&#x76F8;&#x4EA4;&#x9762;&#x79EF;"><i class="fa fa-link" aria-hidden="true"></i></a>4.5. &#x7ED9;&#x5B9A;&#x4E24;&#x77E9;&#x5F62;&#xFF0C;&#x6C42;&#x76F8;&#x4EA4;&#x9762;&#x79EF;</h2>
<ul>
<li><strong>&#x6392;&#x5E8F;&#xFF1A;&#x77E9;&#x5F62;&#x53EF;&#x4EE5;&#x7531;&#x4E24;&#x4E2A;&#x70B9;&#x5BF9;&#x89D2;&#x70B9;&#x8868;&#x793A;&#xFF0C;&#x9996;&#x5148;&#x9700;&#x8981;&#x786E;&#x5B9A;&#x4E24;&#x4E2A;&#x7684;&#x4F4D;&#x7F6E;</strong></li>
<li><strong>&#x4F5C;&#x56FE;&#xFF1A;&#x5224;&#x65AD;&#x76F8;&#x4EA4;&#x5173;&#x7CFB;</strong></li>
</ul>
<pre class="language-"><code class="lang-cpp"><span class="token comment">// &#x786E;&#x4FDD;&#x8D77;&#x70B9;&#x5728;&#x4E0B;&#xFF0C;&#x7EC8;&#x70B9;&#x5728;&#x4E0A;</span>
    Rect <span class="token function">getRect</span><span class="token punctuation">(</span><span class="token keyword">int</span> x1<span class="token punctuation">,</span><span class="token keyword">int</span> y1<span class="token punctuation">,</span><span class="token keyword">int</span> x2<span class="token punctuation">,</span><span class="token keyword">int</span> y2<span class="token punctuation">)</span><span class="token punctuation">{</span>
        Rect A<span class="token punctuation">;</span>
        A<span class="token punctuation">.</span>xStart <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>x1<span class="token punctuation">,</span>x2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        A<span class="token punctuation">.</span>yStart <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>y1<span class="token punctuation">,</span>y2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        A<span class="token punctuation">.</span>xEnd <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>x1<span class="token punctuation">,</span>x2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        A<span class="token punctuation">.</span>yEnd <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>y1<span class="token punctuation">,</span>y2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> A<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> <span class="token function">intersectionRect</span><span class="token punctuation">(</span>Rect A<span class="token punctuation">,</span>Rect B<span class="token punctuation">)</span><span class="token punctuation">{</span>

        <span class="token comment">// &#x5224;&#x65AD;&#x662F;&#x5426;&#x76F8;&#x4EA4;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token punctuation">(</span>A<span class="token punctuation">.</span>xStart <span class="token operator">&gt;=</span> B<span class="token punctuation">.</span>xEnd <span class="token operator">||</span> A<span class="token punctuation">.</span>xEnd <span class="token operator">&lt;=</span> B<span class="token punctuation">.</span>xStart <span class="token operator">||</span> A<span class="token punctuation">.</span>yEnd <span class="token operator">&lt;=</span> B<span class="token punctuation">.</span>yStart <span class="token operator">||</span> A<span class="token punctuation">.</span>yStart <span class="token operator">&gt;=</span> B<span class="token punctuation">.</span>yEnd<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            Rect intersection<span class="token punctuation">;</span>         
            intersection<span class="token punctuation">.</span>xStart <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>A<span class="token punctuation">.</span>xStart<span class="token punctuation">,</span>B<span class="token punctuation">.</span>xStart<span class="token punctuation">)</span><span class="token punctuation">;</span>
            intersection<span class="token punctuation">.</span>yStart <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>A<span class="token punctuation">.</span>yStart<span class="token punctuation">,</span>B<span class="token punctuation">.</span>yStart<span class="token punctuation">)</span><span class="token punctuation">;</span>
            intersection<span class="token punctuation">.</span>xEnd <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>A<span class="token punctuation">.</span>xEnd<span class="token punctuation">,</span>B<span class="token punctuation">.</span>xEnd<span class="token punctuation">)</span><span class="token punctuation">;</span>
            intersection<span class="token punctuation">.</span>yEnd <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>A<span class="token punctuation">.</span>yEnd<span class="token punctuation">,</span>B<span class="token punctuation">.</span>yEnd<span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">return</span> <span class="token punctuation">(</span>intersection<span class="token punctuation">.</span>xEnd <span class="token operator">-</span> intersection<span class="token punctuation">.</span>xStart<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>intersection<span class="token punctuation">.</span>yEnd <span class="token operator">-</span> intersection<span class="token punctuation">.</span>yStart<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h1 id="5-&#x4E8C;&#x5206;&#x6CD5;&#x5A01;&#x529B;&#x52A0;&#x5F3A;"><a name="5-&#x4E8C;&#x5206;&#x6CD5;&#x5A01;&#x529B;&#x52A0;&#x5F3A;" class="anchor-navigation-ex-anchor" href="#5-&#x4E8C;&#x5206;&#x6CD5;&#x5A01;&#x529B;&#x52A0;&#x5F3A;"><i class="fa fa-link" aria-hidden="true"></i></a>5. &#x4E8C;&#x5206;&#x6CD5;&#x5A01;&#x529B;&#x52A0;&#x5F3A;</h1>
<blockquote>
<p>[!tip]
<strong>0. &#x6C42;&#x89E3;&#x8BE5;&#x95EE;&#x9898;&#x53EF;&#x4EE5;&#x5BF9;&#x4E00;&#x4E2A;&#x5355;&#x8C03;&#x51FD;&#x6570;&#x533A;&#x95F4;&#x66B4;&#x529B;&#x904D;&#x5386;</strong></p>
<p><strong>1. &#x89E3;&#x4E3A;&#x4E00;&#x4E2A;&#x5355;&#x8C03;&#x51FD;&#x6570;&#x91CC;&#x7684;&#x67D0;&#x4E00;&#x503C;&#x3002;</strong></p>
<p><strong>2. &#x5355;&#x8C03;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#xFF0C;&#x5C06;&#x533A;&#x95F4;&#x5212;&#x5206;&#x4E3A;&#x4E24;&#x534A;&#x3002;</strong></p>
<p><strong>&#x4E8C;&#x5206;&#x6CD5;&#x627E;&#x503C;&#xFF1A;</strong></p>
<ul>
<li><strong>&#x5355;&#x8C03;&#xFF1A;&#x6570;&#x7EC4;&#x9700;&#x8981;&#x5148;&#x6392;&#x5E8F;</strong></li>
<li><strong>&#x72B6;&#x6001;&#xFF1A;&#x548C;<code>mid</code>&#x6BD4;&#x5927;&#x5C0F;</strong></li>
<li><strong>&#x533A;&#x95F4;&#xFF1A;<code>mid</code>&#x7684;&#x300C;&#x5DE6;&#x8FB9;&#x300D;&#x6216;&#x8005;&#x300C;&#x53F3;&#x8FB9;&#x300D;</strong></li>
</ul>
</blockquote>
<h2 id="51-&#x73C2;&#x73C2;&#x5403;&#x9999;&#x8549;"><a name="51-&#x73C2;&#x73C2;&#x5403;&#x9999;&#x8549;" class="anchor-navigation-ex-anchor" href="#51-&#x73C2;&#x73C2;&#x5403;&#x9999;&#x8549;"><i class="fa fa-link" aria-hidden="true"></i></a>5.1. &#x73C2;&#x73C2;&#x5403;&#x9999;&#x8549;</h2>
<p><a href="../../image/cpp/banana.png" data-lightbox="fa50dd1f-06f9-424e-8a90-9f91c7145115" data-title="banana"><img src="../../image/cpp/banana.png" alt="banana"></a></p>
<ul>
<li><strong><code>calculateH(speed)</code>&#x5355;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x968F;<code>speed</code>&#x589E;&#x52A0;&#x800C;&#x51CF;&#x5C0F;</strong></li>
<li><strong><code>(calculateH(nums,n,mid) &lt;= H)</code>&#x5C06;<code>speed</code>&#x533A;&#x95F4;&#x5212;&#x5206;&#x4E3A;&#x4E24;&#x534A;</strong></li>
</ul>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token function">calculateH</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">int</span><span class="token operator">*</span> nums<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">int</span> speed<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> h <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        h <span class="token operator">+=</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">/</span> speed <span class="token operator">+</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">%</span> speed <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token operator">?</span> <span class="token number">1</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> h<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">minSpeed</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">int</span><span class="token operator">*</span> nums<span class="token punctuation">,</span><span class="token keyword">int</span> n<span class="token punctuation">,</span><span class="token keyword">int</span> H<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">// &#x6700;&#x5927;&#x7684;&#x60C5;&#x51B5;</span>
    <span class="token keyword">int</span> right <span class="token operator">=</span> <span class="token function">findMax</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> 

    <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;=</span> right <span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">int</span> mid <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x901F;&#x5EA6;&#x5FEB;&#x4E86; </span>
        <span class="token comment">// if (calculateH(nums,n,mid) &lt;= H)</span>
        <span class="token comment">// {</span>
        <span class="token comment">//     right = mid - 1;</span>
        <span class="token comment">// }else{</span>
        <span class="token comment">//     left = mid + 1;</span>
        <span class="token comment">// }</span>

        <span class="token comment">// &#x627E;&#x5DE6;&#x8FB9;&#x754C;&#x6807;&#x51C6;&#x5F62;&#x5F0F;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">calculateH</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>n<span class="token punctuation">,</span>left<span class="token punctuation">)</span> <span class="token operator">==</span> H<span class="token punctuation">)</span><span class="token punctuation">{</span>
            right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">calculateH</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>n<span class="token punctuation">,</span>left<span class="token punctuation">)</span> <span class="token operator">&lt;</span> H<span class="token punctuation">)</span><span class="token punctuation">{</span>
            right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">calculateH</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>n<span class="token punctuation">,</span>left<span class="token punctuation">)</span> <span class="token operator">&gt;</span> H<span class="token punctuation">)</span><span class="token punctuation">{</span>
            left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> left<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="52-&#x8FD0;&#x8F93;&#x8D27;&#x7269;"><a name="52-&#x8FD0;&#x8F93;&#x8D27;&#x7269;" class="anchor-navigation-ex-anchor" href="#52-&#x8FD0;&#x8F93;&#x8D27;&#x7269;"><i class="fa fa-link" aria-hidden="true"></i></a>5.2. &#x8FD0;&#x8F93;&#x8D27;&#x7269;</h2>
<p><a href="../../image/cpp/deliver.png" data-lightbox="f4c3442a-6523-4ab4-a0f9-653c54a3b09b" data-title="deliver"><img src="../../image/cpp/deliver.png" alt="deliver"></a></p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">findBorder</span><span class="token punctuation">(</span><span class="token keyword">const</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> goods<span class="token punctuation">,</span><span class="token keyword">int</span> <span class="token operator">&amp;</span> left<span class="token punctuation">,</span><span class="token keyword">int</span> <span class="token operator">&amp;</span> right<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> item<span class="token operator">:</span>goods<span class="token punctuation">)</span><span class="token punctuation">{</span>
        left <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>left<span class="token punctuation">,</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
        right <span class="token operator">+=</span> item<span class="token punctuation">;</span>            
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">calculateDay</span><span class="token punctuation">(</span><span class="token keyword">const</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> goods<span class="token punctuation">,</span><span class="token keyword">int</span> w<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> day <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> item<span class="token operator">:</span>goods<span class="token punctuation">)</span><span class="token punctuation">{</span>
        count <span class="token operator">+=</span> item<span class="token punctuation">;</span>
        <span class="token comment">// &#x8D85;&#x91CD;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>count <span class="token operator">&gt;</span> w<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token comment">// &#x91CD;&#x7F6E;&#xFF0C;&#x5E76;&#x56DE;&#x9000;&#x4E00;&#x4E2A;&#x8D27;&#x7269;</span>
            count <span class="token operator">=</span> item<span class="token punctuation">;</span>
            day <span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x6CA1;&#x88C5;&#x5B8C;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>count <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        day <span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> day<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">minWeight</span><span class="token punctuation">(</span><span class="token keyword">const</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> goods<span class="token punctuation">,</span><span class="token keyword">int</span> day<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> right <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x5BFB;&#x627E;&#x533A;&#x95F4;</span>
    <span class="token function">findBorder</span><span class="token punctuation">(</span>goods<span class="token punctuation">,</span>left<span class="token punctuation">,</span>right<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;=</span> right<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">int</span> mid <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x662F;&#x5426;&#x6EE1;&#x8DB3;&#x5929;&#x6570;&#x8981;&#x6C42;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">calculateDay</span><span class="token punctuation">(</span>goods<span class="token punctuation">,</span>mid<span class="token punctuation">)</span> <span class="token operator">==</span> day<span class="token punctuation">)</span><span class="token punctuation">{</span>
            right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">calculateDay</span><span class="token punctuation">(</span>goods<span class="token punctuation">,</span>mid<span class="token punctuation">)</span> <span class="token operator">&lt;</span> day<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            right <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">calculateDay</span><span class="token punctuation">(</span>goods<span class="token punctuation">,</span>mid<span class="token punctuation">)</span> <span class="token operator">&gt;</span> day<span class="token punctuation">)</span><span class="token punctuation">{</span>
            left <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> left<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="53-&#x5206;&#x5272;&#x6570;&#x7EC4;&#x7684;&#x6700;&#x5927;&#x503C;"><a name="53-&#x5206;&#x5272;&#x6570;&#x7EC4;&#x7684;&#x6700;&#x5927;&#x503C;" class="anchor-navigation-ex-anchor" href="#53-&#x5206;&#x5272;&#x6570;&#x7EC4;&#x7684;&#x6700;&#x5927;&#x503C;"><i class="fa fa-link" aria-hidden="true"></i></a>5.3. &#x5206;&#x5272;&#x6570;&#x7EC4;&#x7684;&#x6700;&#x5927;&#x503C;</h2>
<p><strong>&#x95EE;&#x9898;<code>5.2</code>&#x7684;&#x53D8;&#x79CD;&#x3002;</strong></p>
<p><a href="../../image/cpp/spliteArrayMax.png" data-lightbox="71ecf0ff-5d4c-49b7-aa65-e247616ee67f" data-title="spit array"><img src="../../image/cpp/spliteArrayMax.png" alt="spit array"></a></p>
<h1 id="6-&#x6ED1;&#x52A8;&#x7A97;&#x53E3;"><a name="6-&#x6ED1;&#x52A8;&#x7A97;&#x53E3;" class="anchor-navigation-ex-anchor" href="#6-&#x6ED1;&#x52A8;&#x7A97;&#x53E3;"><i class="fa fa-link" aria-hidden="true"></i></a>6. &#x6ED1;&#x52A8;&#x7A97;&#x53E3;</h1>
<blockquote>
<p>[!note|style:flat]
<strong>&#x6ED1;&#x52A8;&#x7A97;&#x53E3;&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x89E3;&#x51B3;&#xFF0C;&#x5728;&#x957F;&#x5E8F;&#x5217;&#x4E0A;&#xFF0C;&#x4E0D;&#x8981;&#x6C42;&#x987A;&#x5E8F;&#xFF0C;&#x627E;&#x5B50;&#x5E8F;&#x5217;&#x7684;&#x5404;&#x79CD;&#x95EE;&#x9898;&#xFF0C;&#x3002;</strong></p>
</blockquote>
<h2 id="61-&#x6A21;&#x677F;"><a name="61-&#x6A21;&#x677F;" class="anchor-navigation-ex-anchor" href="#61-&#x6A21;&#x677F;"><i class="fa fa-link" aria-hidden="true"></i></a>6.1. &#x6A21;&#x677F;</h2>
<blockquote>
<p>[!tip]
<strong>&#x53EF;&#x4EE5;&#x60F3;&#x8C61;&#x6210;&#x4E00;&#x6761;&#x866B;&#x5B50;&#x5728;&#x4E00;&#x6839;&#x7EBF;&#x4E0A;&#x4ECE;&#x5DE6;&#x5411;&#x53F3;&#x8815;&#x52A8;&#x3002;</strong></p>
<ul>
<li><strong>&#x5148;&#x5934;&#x52A8;&#xFF0C;&#x641C;&#x5BFB;&#x95EE;&#x9898;&#x7684;&#x89E3;&#xFF0C;&#x66F4;&#x65B0;&#x72B6;&#x6001;</strong></li>
<li><strong>&#x540E;&#x5C3E;&#x52A8;&#xFF0C;&#x4F18;&#x5316;&#x627E;&#x5230;&#x7684;&#x89E3;&#xFF0C;&#x66F4;&#x65B0;&#x72B6;&#x6001;&#x3002;</strong></li>
</ul>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> right <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">&lt;</span> s<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x589E;&#x5927;&#x7A97;&#x53E3;</span>
        window<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        right<span class="token operator">++</span><span class="token punctuation">;</span>

        <span class="token keyword">while</span> <span class="token punctuation">(</span>window needs shrink<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x7F29;&#x5C0F;&#x7A97;&#x53E3;</span>
            window<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>s<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            left<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-cpp"><span class="token comment">/* &#x6ED1;&#x52A8;&#x7A97;&#x53E3;&#x7B97;&#x6CD5;&#x6846;&#x67B6; */</span>
<span class="token keyword">void</span> <span class="token function">slidingWindow</span><span class="token punctuation">(</span>string s<span class="token punctuation">,</span> string t<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// &#x6807;&#x8BB0;&#x5E8F;&#x5217;&#x7528;</span>
    <span class="token comment">// window&#xFF1A;&#x6807;&#x8BB0;&#x4E3B;&#x5E8F;&#x5217;&#xFF1B;need&#xFF1A;&#x6807;&#x8BB0;&#x5B50;&#x5E8F;&#x5217;</span>
    unordered_map<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">&gt;</span> need<span class="token punctuation">,</span> window<span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">char</span> c <span class="token operator">:</span> t<span class="token punctuation">)</span> need<span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x7A97;&#x7684;&#x5DE6;&#x53F3;&#x8FB9;&#x754C;</span>
    <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> right <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x6709;&#x6548;&#x4FE1;&#x606F;&#x7684;&#x6807;&#x8BB0;&#x503C;</span>
    <span class="token keyword">int</span> valid <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 

    <span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">&lt;</span> s<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// c &#x662F;&#x5C06;&#x79FB;&#x5165;&#x7A97;&#x53E3;&#x7684;&#x5B57;&#x7B26;</span>
        <span class="token keyword">char</span> c <span class="token operator">=</span> s<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token comment">// &#x53F3;&#x79FB;&#x7A97;&#x53E3;</span>
        right<span class="token operator">++</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x6269;&#x5C55;&#x7A97;&#x53E3;</span>
        window<span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x8FDB;&#x884C;&#x7A97;&#x53E3;&#x5185;&#x6570;&#x636E;&#x7684;&#x4E00;&#x7CFB;&#x5217;&#x66F4;&#x65B0;</span>
        <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

        <span class="token comment">/*** debug &#x8F93;&#x51FA;&#x7684;&#x4F4D;&#x7F6E; ***/</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;window: [%d, %d)\n&quot;</span><span class="token punctuation">,</span> left<span class="token punctuation">,</span> right<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">/********************/</span>

        <span class="token comment">// &#x5224;&#x65AD;&#x5DE6;&#x4FA7;&#x7A97;&#x53E3;&#x662F;&#x5426;&#x8981;&#x6536;&#x7F29;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>window needs shrink<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// d &#x662F;&#x5C06;&#x79FB;&#x51FA;&#x7A97;&#x53E3;&#x7684;&#x5B57;&#x7B26;</span>
            <span class="token keyword">char</span> d <span class="token operator">=</span> s<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x5DE6;&#x79FB;&#x7A97;&#x53E3;</span>
            left<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x8FDB;&#x884C;&#x7A97;&#x53E3;&#x5185;&#x6570;&#x636E;&#x7684;&#x4E00;&#x7CFB;&#x5217;&#x66F4;&#x65B0;</span>
            <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

            <span class="token comment">// &#x7F29;&#x5C0F;&#x7A97;&#x53E3;</span>
            window<span class="token punctuation">[</span>d<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>[!warning|style:flat]
<strong><code>window[]</code>&#x5728;&#x6269;&#x5C55;&#x7A97;&#x53E3;&#x65F6;&#xFF0C;&#x6700;&#x5148;&#x66F4;&#x65B0;&#x3002;</strong></p>
<p><strong><code>window[]</code>&#x5728;&#x6536;&#x7F29;&#x7A97;&#x53E3;&#x65F6;&#xFF0C;&#x6700;&#x540E;&#x66F4;&#x65B0;&#x3002;</strong></p>
</blockquote>
<h2 id="62-&#x6700;&#x5C0F;&#x8986;&#x76D6;&#x5B50;&#x4E32;"><a name="62-&#x6700;&#x5C0F;&#x8986;&#x76D6;&#x5B50;&#x4E32;" class="anchor-navigation-ex-anchor" href="#62-&#x6700;&#x5C0F;&#x8986;&#x76D6;&#x5B50;&#x4E32;"><i class="fa fa-link" aria-hidden="true"></i></a>6.2. &#x6700;&#x5C0F;&#x8986;&#x76D6;&#x5B50;&#x4E32;</h2>
<p><a href="../../image/cpp/coveredSub.png" data-lightbox="89e9b5ba-646a-485d-ba1f-f357fedad618" data-title="covered sub"><img src="../../image/cpp/coveredSub.png" alt="covered sub"></a></p>
<pre class="language-"><code class="lang-cpp">string <span class="token function">coveredSubsequences</span><span class="token punctuation">(</span><span class="token keyword">const</span> string <span class="token operator">&amp;</span>s<span class="token punctuation">,</span><span class="token keyword">const</span> string <span class="token operator">&amp;</span>t<span class="token punctuation">)</span><span class="token punctuation">{</span>
    unordered_map<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">&gt;</span> windows<span class="token punctuation">;</span>
    unordered_map<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">&gt;</span> need<span class="token punctuation">;</span>

    <span class="token comment">// &#x6807;&#x8BB0;&#x5B57;&#x4E32;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">char</span> ch <span class="token operator">:</span> t<span class="token punctuation">)</span><span class="token punctuation">{</span>
        need<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> len <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> start <span class="token operator">=</span> len<span class="token punctuation">;</span>

    <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> right <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> valid <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x5934;&#x632A;&#x52A8;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">&lt;</span> s<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// &#x8FDB;&#x6765;&#x4E00;&#x4E2A;</span>
        <span class="token keyword">char</span> inch <span class="token operator">=</span> s<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span> 
        right<span class="token operator">++</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x7A97;&#x4F53;&#x5185;&#x5BB9;&#x6807;&#x8BB0;</span>
        windows<span class="token punctuation">[</span>inch<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x6709;&#x6548;&#x4FE1;&#x606F;&#x8BB0;&#x5F55;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span> need<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>inch<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> windows<span class="token punctuation">[</span>inch<span class="token punctuation">]</span> <span class="token operator">==</span> need<span class="token punctuation">[</span>inch<span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            valid<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// &#x6EE1;&#x8DB3;&#x6700;&#x4F4E;&#x9650;&#x5EA6;&#x4E86;&#xFF0C;&#x79FB;&#x52A8;&#x5C3E;&#x5DF4;&#x4F18;&#x5316;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>valid <span class="token operator">==</span> need<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">// &#x50A8;&#x5B58;&#x6700;&#x4F18;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>len <span class="token operator">&gt;</span> right <span class="token operator">-</span> left<span class="token punctuation">)</span><span class="token punctuation">{</span>
                len <span class="token operator">=</span> right <span class="token operator">-</span> left<span class="token punctuation">;</span>
                start <span class="token operator">=</span> left<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// &#x51FA;&#x53BB;&#x4E00;&#x4E2A;</span>
            <span class="token keyword">char</span> outch <span class="token operator">=</span> s<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">;</span>
            left<span class="token operator">++</span><span class="token punctuation">;</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>need<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>outch<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> windows<span class="token punctuation">[</span>outch<span class="token punctuation">]</span> <span class="token operator">==</span> need<span class="token punctuation">[</span>outch<span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                valid<span class="token operator">--</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// &#x64CD;&#x4F5C;&#x5B8C;&#x6BD5;&#x518D;&#x53D6;&#x6D88;</span>
            windows<span class="token punctuation">[</span>outch<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>len <span class="token operator">==</span> s<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> s<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span>len<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre>
<h2 id="63-&#x5B57;&#x7B26;&#x4E32;&#x6392;&#x5217;"><a name="63-&#x5B57;&#x7B26;&#x4E32;&#x6392;&#x5217;" class="anchor-navigation-ex-anchor" href="#63-&#x5B57;&#x7B26;&#x4E32;&#x6392;&#x5217;"><i class="fa fa-link" aria-hidden="true"></i></a>6.3. &#x5B57;&#x7B26;&#x4E32;&#x6392;&#x5217;</h2>
<p style="text-align:center;"><a href="../../image/cpp/stringSort.jpg" data-lightbox="87c4d507-eb44-466b-becb-7393a9543a2b" data-title="undefined"><img src="../../image/cpp/stringSort.jpg" alt="undefined"></a></p>

<pre class="language-"><code class="lang-cpp"><span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">&lt;</span> S<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">char</span> ch <span class="token operator">=</span> S<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
        right<span class="token operator">++</span><span class="token punctuation">;</span>
        &#x3002;&#x3002;&#x3002;
        <span class="token comment">// &#x627E;&#x5230;&#x89E3;&#x7684;&#x8BC4;&#x5224;&#x6807;&#x51C6;&#x662F;&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left <span class="token operator">&gt;=</span> B<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token comment">// &#x5B57;&#x7B26;&#x90FD;&#x627E;&#x5230;&#x4E86;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>valid <span class="token operator">==</span> need<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            &#x3002;&#x3002;&#x3002;   
    <span class="token punctuation">}</span>
</code></pre>
<ul>
<li><strong><code>right - left &gt;= B.length()</code>:&#x4F7F;&#x7528;<code>&gt;=</code>&#xFF0C;&#x4E0D;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x65F6;&#xFF0C;&#x8FD8;&#x8981;&#x7F29;&#x5C0F;&#x7A97;&#x53E3;&#x3002;</strong></li>
</ul>
<sec data-title="&#x5B8C;&#x6574;&#x4EE3;&#x7801;" data-id="section0" data-show="true" data-collapse="true"><div class="panel panel-default"><div class="panel-heading"><b>&#x5B8C;&#x6574;&#x4EE3;&#x7801;<a class="pull-right section atTitle btn btn-default" target="section0"><span class="fa fa-angle-up"></span></a></b></div><div class="panel-collapse in" id="section0"><div class="panel-body">
<pre class="language-"><code class="lang-cpp"><span class="token keyword">bool</span> <span class="token function">substring</span><span class="token punctuation">(</span><span class="token keyword">const</span> string <span class="token operator">&amp;</span> S<span class="token punctuation">,</span><span class="token keyword">const</span> string <span class="token operator">&amp;</span> B<span class="token punctuation">)</span><span class="token punctuation">{</span>

    unordered_map<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">&gt;</span> window<span class="token punctuation">,</span>need<span class="token punctuation">;</span>

    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">char</span> ch<span class="token operator">:</span>B<span class="token punctuation">)</span><span class="token punctuation">{</span>
        need<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> right <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">int</span> valid <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">&lt;</span> S<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">char</span> ch <span class="token operator">=</span> S<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
        right<span class="token operator">++</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x5B58;&#x5728;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>need<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x6EE1;&#x8DB3;&#x8981;&#x6C42;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span> <span class="token operator">==</span> need<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                valid <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> 
        <span class="token punctuation">}</span>

        <span class="token comment">// &#x627E;&#x5230;&#x89E3;&#x7684;&#x8BC4;&#x5224;&#x6807;&#x51C6;&#x662F;&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left <span class="token operator">&gt;=</span> B<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>valid <span class="token operator">==</span> need<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">char</span> ch <span class="token operator">=</span> S<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">;</span>
            left<span class="token operator">++</span><span class="token punctuation">;</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>need<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span> <span class="token operator">==</span> need<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                    valid<span class="token operator">--</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>    
    <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</div></div></div></sec>
<h2 id="64-&#x627E;&#x6240;&#x6709;&#x5B57;&#x6BCD;&#x5F02;&#x4F4D;&#x8BCD;"><a name="64-&#x627E;&#x6240;&#x6709;&#x5B57;&#x6BCD;&#x5F02;&#x4F4D;&#x8BCD;" class="anchor-navigation-ex-anchor" href="#64-&#x627E;&#x6240;&#x6709;&#x5B57;&#x6BCD;&#x5F02;&#x4F4D;&#x8BCD;"><i class="fa fa-link" aria-hidden="true"></i></a>6.4. &#x627E;&#x6240;&#x6709;&#x5B57;&#x6BCD;&#x5F02;&#x4F4D;&#x8BCD;</h2>
<p><a href="../../image/cpp/diffchar.png" data-lightbox="008945d8-4b1c-48b6-9e11-2ceab360256c" data-title="diff char"><img src="../../image/cpp/diffchar.png" alt="diff char"></a></p>
<p><strong>&#x89E3;&#x9898;&#x601D;&#x8DEF;&#x548C;<code>6.3</code>&#x4E00;&#x6837;&#x3002;</strong></p>
<sec data-title="&#x5B8C;&#x6574;&#x4EE3;&#x7801;" data-id="section1" data-show="true" data-collapse="true"><div class="panel panel-default"><div class="panel-heading"><b>&#x5B8C;&#x6574;&#x4EE3;&#x7801;<a class="pull-right section atTitle btn btn-default" target="section1"><span class="fa fa-angle-up"></span></a></b></div><div class="panel-collapse in" id="section1"><div class="panel-body">
<pre class="language-"><code class="lang-cpp">vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token function">diffSubstring</span><span class="token punctuation">(</span><span class="token keyword">const</span> string<span class="token operator">&amp;</span> s1<span class="token punctuation">,</span> <span class="token keyword">const</span> string<span class="token operator">&amp;</span> s2<span class="token punctuation">)</span><span class="token punctuation">{</span>

    <span class="token comment">// &#x5B58;&#x50A8;&#x7D22;&#x5F15;</span>
    vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> starts<span class="token punctuation">;</span>

    unordered_map<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">&gt;</span> need<span class="token punctuation">,</span>window<span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">char</span> ch<span class="token operator">:</span>s2<span class="token punctuation">)</span><span class="token punctuation">{</span>
        need<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> left<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> right<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> valid <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span><span class="token punctuation">(</span>right <span class="token operator">&lt;</span> s1<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">char</span> ch <span class="token operator">=</span> s1<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
        right<span class="token operator">++</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>need<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span> <span class="token operator">==</span> need<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                valid<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>


        <span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left <span class="token operator">&gt;=</span> s2<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>valid <span class="token operator">==</span> need<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                starts<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>left<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">char</span> ch <span class="token operator">=</span> s1<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">;</span>
            left<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>need<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>need<span class="token punctuation">[</span>ch<span class="token punctuation">]</span> <span class="token operator">==</span> window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    valid<span class="token operator">--</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> starts<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</div></div></div></sec>
<h2 id="65-&#x6700;&#x957F;&#x65E0;&#x91CD;&#x590D;&#x5B50;&#x4E32;"><a name="65-&#x6700;&#x957F;&#x65E0;&#x91CD;&#x590D;&#x5B50;&#x4E32;" class="anchor-navigation-ex-anchor" href="#65-&#x6700;&#x957F;&#x65E0;&#x91CD;&#x590D;&#x5B50;&#x4E32;"><i class="fa fa-link" aria-hidden="true"></i></a>6.5. &#x6700;&#x957F;&#x65E0;&#x91CD;&#x590D;&#x5B50;&#x4E32;</h2>
<p><a href="../../image/cpp/maxLenSubstring.png" data-lightbox="6adeab0e-5f47-48cd-90b4-8f7c3f4e3622" data-title="max len substr"><img src="../../image/cpp/maxLenSubstring.png" alt="max len substr"></a></p>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span> <span class="token function">findMaxLen</span><span class="token punctuation">(</span><span class="token keyword">const</span> string<span class="token operator">&amp;</span> str<span class="token punctuation">)</span><span class="token punctuation">{</span>
    unordered_map<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">&gt;</span> window<span class="token punctuation">;</span>

    <span class="token keyword">int</span> left<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> right<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">int</span> maxLen <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span><span class="token punctuation">(</span>right <span class="token operator">&lt;</span> str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">char</span> ch <span class="token operator">=</span> str<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
        right<span class="token operator">++</span><span class="token punctuation">;</span>
        window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x53BB;&#x6389;&#x91CD;&#x590D;&#x7684;&#x5B57;&#x7B26;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>window<span class="token punctuation">[</span>ch<span class="token punctuation">]</span> <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">char</span> out <span class="token operator">=</span> str<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">;</span>
            left<span class="token operator">++</span><span class="token punctuation">;</span>
            window<span class="token punctuation">[</span>out<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span> 
        <span class="token punctuation">}</span>

        maxLen <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>maxLen<span class="token punctuation">,</span>right <span class="token operator">-</span> left<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> maxLen<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="array_list_withNum.html" class="navigation navigation-prev " aria-label="Previous page: 十、数组链表操作">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="tree_withNum.html" class="navigation navigation-next " aria-label="Next page: 十二、树">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"十一、目标查找","level":"2.1.11","depth":2,"next":{"title":"十二、树","level":"2.1.12","depth":2,"path":"cpp/chapter/tree_withNum.md","ref":"cpp/chapter/tree_withNum.md","articles":[]},"previous":{"title":"十、数组链表操作","level":"2.1.10","depth":2,"path":"cpp/chapter/array_list_withNum.md","ref":"cpp/chapter/array_list_withNum.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro-fixed","expandable-chapters-small","ancre-navigation","hide-element","code","splitter","insert-logo-link-website","-sharing","sharing-plus","prism","-highlight","flexible-alerts","lightbox","pageview-count","auto-scroll-table","katex-pro","favicon-absolute","sectionx","terminull-light","livereload"],"styles":{"website":"styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"prism":{"css":["prismjs/themes/prism-tomorrow.css"],"lang":{"flow":"typescript"}},"livereload":{},"splitter":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"auto-scroll-table":{},"code":{"copyButtons":true},"terminull-light":{},"search-pro-fixed":{},"favicon-absolute":{"favicon":"image/ico.png","appleTouchIconPrecomposed152":"image/ico.png","appleTouchIconMore":{},"appleTouchIconPrecomposedMore":{}},"hide-element":{"elements":[".gitbook-link"]},"katex-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"sectionx":{"tag":"b"},"lightbox":{"jquery":true,"sameUuid":false},"insert-logo-link-website":{"url":"image/logo.png","link":"https://space.bilibili.com/27206875","target":"_blank","style":"height: 230px"},"pageview-count":{},"ancre-navigation":{"showLevel":true,"associatedWithSummary":true,"mode":"float","printLog":false,"multipleH1":true,"float":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"expandable-chapters-small":{},"flexible-alerts":{"style":"callout","note":{"label":"Note","icon":"fa fa-info-circle","className":"info"},"tip":{"label":"Tip","icon":"fa fa-lightbulb-o","className":"tip"},"warning":{"label":"Warning","icon":"fa fa-exclamation-triangle","className":"warning"},"danger":{"label":"Attention","icon":"fa fa-ban","className":"danger"}},"sharing":{"qq":true,"all":["douban","facebook","google","instapaper","linkedin","twitter","weibo","messenger","qq","qzone","viber","whatsapp"],"douban":false,"facebook":false,"weibo":true,"whatsapp":false,"twitter":false,"pocket":false,"google":true,"qzone":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"海拉尔的三角","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"计算机笔记","links":{"sidebar":{"Github":"https://github.com/spite-triangle"}},"gitbook":"*"},"file":{"path":"cpp/chapter/find_withNum.md","mtime":"2021-09-14T15:43:12.376Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-09-14T15:43:23.052Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro-fixed/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro-fixed/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-insert-logo-link-website/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-flexible-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-auto-scroll-table/plugin.js"></script>
        
    
        
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sectionx/sectionx.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-terminull-light/terminull.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

